import axios from "axios";

const ajax = axios.create({
	baseURL: "http://localhost:3000", // 请求基本路径
	timeout: 5000, // 请求超时
	withCredentials: true
});

ajax.interceptors.request.use(config => {
	return config;
});

ajax.interceptors.response.use(
	response => {
		const res = response.data;
		// 判断后端返回数据是否成功
		if (res.code !== 0) {
			// 统一提示错误
			uni.showToast({
				title: res.msg,
				icon: "error",
			});
			return Promise.reject(new Error(res.message || "Error"));
		} else {
			return res;
		}
	},
	error => {
		uni.showToast({
			title: "error",
			icon: "error",
			mask: true,
		});
		console.log(error);
		return new Promise(() => {});
	}
);

export default ajax;
